import Vue from 'vue'
import Router from 'vue-router'
import BaseFramework from 'user/BaseFramework'
import 'styles/reset.css'
import store from '@/store'
Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Index',
      component: () => import('@/pages/index/Index'),
      meta: {
        title: '首页'
      }
    },
    {
      path: '/basefw',
      name: 'BaseFramework',
      component: BaseFramework,
      redirect: '/userindex',
      meta: {
        title: '个人中心',
        requiresAuth: true
      },
      children: [
        {
          path: '/userindex',
          name: 'UserIndex',
          component: () => import('user/userindex/UserIndex'),
          meta: {
            title: '一码通首页',
            requiresAuth: true
          }
        },
        {
          path: '/licenseeslist',
          name: 'LicenseesList',
          component: () => import('user/licenseeslist/licenseesList'),
          meta: {
            title: '被授权商列表',
            requiresAuth: true
          }
        },
        {
          path: '/prolist',
          name: 'ProList',
          component: () => import('user/prolist/ProList'),
          meta: {
            title: '商品列表',
            requiresAuth: true
          }
        },
        {
          path: '/prodetail',
          name: 'ProDetail',
          component: () => import('user/prolist/ProDetail'),
          meta: {
            title: '商品详情',
            requiresAuth: true
          }
        }
      ]
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('pages/login/Login')
    },
    {
      path: '/resetpwd',
      name: 'ResetPwd',
      component: () => import('pages/login/ResetPwd')
    },
    {
      path: '/resetPwdSucc',
      name: 'ResetpwdSucc',
      component: () => import('pages/login/ResetpwdSucc')
    },
    {
      path: '/newpower',
      name: 'newpower',
      component: () => import('pages/newpower/NewPower'),
      meta: {
        title: '新增授权',
        requiresAuth: true
      }
    }
  ]
})
//  路由拦截:
//  注册全局钩子用来拦截导航
router.beforeEach((to, from, next) => {
  //  获取store里面的token
  let token = store.state.token
  //  判断要去的路由有没有requiresAuth
  if (to.meta.requiresAuth) {
    if (token) {
      next()
    } else {
      //  window.location.href = 'https://www.baidu.com'
      next({
        //  path: 'https://www.baidu.com',
        //  将刚刚要去的路由path（却无权限）作为参数，方便登录成功后直接跳转到该路由
        query: { redirect: to.fullPath }
      })
    }
  } else {
    //  如果无需token,那么随它去吧
    next()
  }
})
export default router
